home *** CD-ROM | disk | FTP | other *** search
/ Sun Solutions 1997 April to September / Sun Solutions CD - APR '97 - SEP '97 (704-3778-12 Rev. H)(Sun Microsystems, Inc.)(1997).iso / products / OpenLink / sample.java < prev    next >
Text File  |  1997-01-17  |  4KB  |  140 lines

  1. // =============================================================
  2. //
  3. // OpenLink Java/JDBC Sample program.
  4. //
  5. // version 1.0
  6. //
  7. // Note. This sample program will connect over the 
  8. // internet to retrieve data with no additional
  9. // configuration needed.
  10. //
  11. // All you need to do is to ensure that the openlink
  12. // jdbc drivers are in the classpath:
  13. //
  14. // CLASSPATH=$CLASSPATH;/java/lib/opljdbc.zip
  15. // or 
  16. // CLASSPATH=%CLASSPATH%;C:\java\lib\opljdbc.zip
  17. //
  18. // depending on your environment and location of opljdbc files.
  19. //
  20. // =============================================================
  21.  
  22.  
  23. // Setup the imports for the classfile.
  24. import java.util.Properties;
  25. import java.sql.*;
  26.  
  27. //declare the class for our example
  28. class sample
  29.   {
  30.  
  31. //Routine to display the contents of a field.
  32.   public static void PrintField (String name)
  33.     {
  34.       if (name == null)
  35.         name = "NULL";
  36.       System.out.print (name + " ");
  37.     }
  38.  
  39. //Routine to execute a query serverside via the openlink jdbc driver
  40.   public static void
  41.   ExecuteQuery (Connection conn, String query)
  42.       throws Exception
  43.     {
  44.       // declare local objects
  45.       ResultSetMetaData meta;
  46.       Statement stmt;
  47.       ResultSet result;
  48.       int count;
  49.  
  50.       // Screen comment...
  51.       System.out.println ("EXECUTE: " + query);
  52.  
  53.       // Execute Query
  54.       stmt = conn.createStatement ();
  55.       result = stmt.executeQuery (query);
  56.  
  57.       // Get Resultset information
  58.       meta = result.getMetaData ();
  59.       count = meta.getColumnCount ();
  60.       
  61.       // Display Column Names of table
  62.       for (int c = 1; c <= count; c++)
  63.     PrintField (meta.getColumnName (c));
  64.       System.out.println ("\n--------------");
  65.  
  66.       // Retrieve results
  67.       while (result.next())
  68.         {
  69.       for (int c = 1; c <= count; c++)
  70.         // Print Results on screen
  71.             PrintField (result.getString (c));
  72.       System.out.println ("");
  73.     }
  74.       stmt.close ();
  75.       System.out.println ("");
  76.     }
  77.  
  78.   // Program entry point
  79.   public static void main (String argv[])
  80.       throws Exception
  81.     {
  82.       Connection conn;
  83.  
  84.       // Add the OpenLink JDBC driver to the system properties
  85.       Properties p = System.getProperties ();
  86.       p.put ("jdbc.drivers", "openlink.jdbc.Driver");
  87.       System.setProperties (p);
  88.  
  89.       // Connect to the Request Broker.
  90.       //    
  91.       // If you have an internet connection, this 
  92.       // URL will retrieve data across the internet
  93.       // from our UK Based Request Broker. 
  94.       //
  95.       // The JDBC Agent is configured for a secure connection
  96.       // and will only connect to a low-privelege user
  97.       // on an Oracle (NT) database. You have select 
  98.       // insert, update, create table and drop table
  99.       // privileges. You have access to the following
  100.       // tables: emp, dept, bonus, so the following queries
  101.       // should produce data:
  102.       //
  103.       // select * from emp
  104.       // select * from dept
  105.       // select * from bonus
  106.       //
  107.       // Note that the URL does not contain a username (UID) or password
  108.       // (PWD) or any other connection information. 
  109.       // 
  110.       // This is because the JDBC Agent is configured in +secure mode, 
  111.       // and will ignore all other connection information other than the 
  112.       // DataSourceName. The remainder of the connection information is 
  113.       // gathered serverside from the OpenLink Rulebook. If the +secure
  114.       // option is _not_ used, it would be possible to submit connection 
  115.       // information in the URL as per the documentation.
  116.  
  117.       // Note also, the initial connection to the broker is on port 
  118.       // 5000. This is the default for OpenLink JDBC. It is configurable.
  119.  
  120.       // If there is a firewall in place, you will need to punch a hole 
  121.       // for ports 5000 through 6000. For connections to the Request
  122.       // Broker, the initial connection is managed through port 
  123.       // 5000 (default). The client classes will then connect to the 
  124.       // jdbc agent on the next lowest available port within the range 
  125.       // specified in the OpenLink Rulebook between the PortLow and 
  126.       // PortHigh settings.
  127.     
  128.       conn = DriverManager.getConnection (
  129.                   "jdbc:openlink://oplweb.openlink.co.uk/DSN=publicDSN_Oracle/UID=jdbc/PWD=java");
  130.  
  131.       // Assumes Oracle database ...
  132.       for (int i = 0; i < 10; i++)
  133.  
  134.         // Execute Query: This can be changed.....
  135.  
  136.         // ExecuteQuery (conn, "select * from dept");
  137.         ExecuteQuery (conn, "select * from emp");
  138.     }
  139. }
  140.